package com.lty.jeeg.server.modules.fas.controller;

import com.lty.jeeg.server.common.annotation.LogAnnotation;
import com.lty.jeeg.server.common.dto.ResponseInfo;
import com.lty.jeeg.server.common.enums.ResponseEnum;
import com.lty.jeeg.server.common.excel.EasyExcelUtil;
import com.lty.jeeg.server.common.page.table.PageTableRequest;
import com.lty.jeeg.server.common.page.table.PageTableResponse;
import com.lty.jeeg.server.common.utils.BeanConverter;
import com.lty.jeeg.server.common.utils.DateUtils;
import com.lty.jeeg.server.common.utils.ResponseUtil;
import com.lty.jeeg.server.modules.fas.dto.HandOverDTO;
import com.lty.jeeg.server.modules.fas.export.HandOverExportEntity;
import com.lty.jeeg.server.modules.fas.model.HandOver;
import com.lty.jeeg.server.modules.fas.service.HandOverService;
import com.lty.jeeg.server.modules.fas.vo.HandOverVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author : zl
 * @date : 2019-02-13 14:56:11
 * Description : 出入库
 */
@Api(tags = "出入库")
@RestController
@RequestMapping("/handOver")
public class HandOverController {

    private final HandOverService handOverService;

    @Autowired
    public HandOverController(HandOverService handOverService) {
        this.handOverService = handOverService;
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id获取")
    public ResponseInfo get(@PathVariable Long id) {
        return ResponseUtil.success(handOverService.getById(id));
    }

    @PostMapping
    @ApiOperation(value = "保存")
    public ResponseInfo save(@RequestBody HandOver handOver) {
        ResponseEnum responseEnum = handOverService.save(handOver);
        if (responseEnum.getCode() == 0) {
            return ResponseUtil.success(responseEnum);
        } else {
            return ResponseUtil.error(responseEnum);
        }
    }

    @PutMapping
    @ApiOperation(value = "修改")
    public ResponseInfo update(@RequestBody HandOver handOver) {
        return ResponseUtil.success(handOverService.update(handOver));
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
    public ResponseInfo delete(@PathVariable Long id) {
        return ResponseUtil.success(handOverService.delete(id));
    }

    @GetMapping("/list")
    @ApiOperation(value = "列表")
    public PageTableResponse list(PageTableRequest request) {
        return handOverService.list(request);
    }


    @PostMapping("/discard")
    @ApiOperation(value = "报废或置换")
    @RequiresPermissions("fas:discard:add")
    public ResponseInfo discard(@RequestBody HandOver handOver) {
        ResponseEnum em = handOverService.discard(handOver);
        if (em.getCode() == 0) {
            return ResponseUtil.success(em);
        } else {
            return ResponseUtil.error(em);
        }
    }

    @LogAnnotation
    @PostMapping("/export/{type}")
    @ApiOperation(value = "出入库记录导出")
    @RequiresPermissions("fas:handOver:export")
    public void export(@PathVariable Integer type, HandOverVO handOverVO, HttpServletResponse response) {
        HandOverDTO handOverDTO;
        if (type == 1) {
            handOverDTO = BeanConverter.map(handOverVO, HandOverDTO.class);
        } else {
            handOverDTO = new HandOverDTO();
            handOverDTO.setId(1L);
        }
        List<HandOverExportEntity> dataList = handOverService.getExportDatas(handOverDTO);
        String fileName = "出入库记录列表导出" + DateUtils.getDateTime("yyyyMMddHHmmss");
        EasyExcelUtil.writeExcel(response, dataList, fileName, "出入库记录列表", new HandOverExportEntity());
    }

    @PostMapping("/approval")
    @ApiOperation(value = "报废或置换的审批")
    public ResponseInfo approval(HttpServletRequest request) {
        ResponseEnum em = handOverService.approval(request);
        if (em.getCode() == 0) {
            return ResponseUtil.success(em);
        } else {
            return ResponseUtil.error(em);
        }
    }

}
